import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TicketDAO {

    // 添加电影票
    public boolean addTicket(Ticket ticket) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO tickets (id, scheduleId, movieId, seatNumber, price, username, purchaseTime) VALUES (?, ?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, ticket.getId());
            pstmt.setString(2, ticket.getScheduleId());
            pstmt.setString(3, ticket.getMovieId());
            pstmt.setString(4, ticket.getSeatNumber());
            pstmt.setDouble(5, ticket.getPrice());
            pstmt.setString(6, ticket.getUsername());
            pstmt.setString(7, ticket.getPurchaseTime());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("添加电影票失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 根据ID查找电影票
    public Ticket findTicketById(String id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Ticket ticket = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM tickets WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                ticket = new Ticket();
                ticket.setId(rs.getString("id"));
                ticket.setScheduleId(rs.getString("scheduleId"));
                ticket.setMovieId(rs.getString("movieId"));
                ticket.setSeatNumber(rs.getString("seatNumber"));
                ticket.setPrice(rs.getDouble("price"));
                ticket.setUsername(rs.getString("username"));
                ticket.setPurchaseTime(rs.getString("purchaseTime"));
            }
        } catch (SQLException e) {
            System.out.println("查找电影票失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return ticket;
    }

    // 根据用户名查找电影票
    public List<Ticket> findTicketsByUsername(String username) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Ticket> tickets = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM tickets WHERE username = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Ticket ticket = new Ticket();
                ticket.setId(rs.getString("id"));
                ticket.setScheduleId(rs.getString("scheduleId"));
                ticket.setMovieId(rs.getString("movieId"));
                ticket.setSeatNumber(rs.getString("seatNumber"));
                ticket.setPrice(rs.getDouble("price"));
                ticket.setUsername(rs.getString("username"));
                ticket.setPurchaseTime(rs.getString("purchaseTime"));
                tickets.add(ticket);
            }
        } catch (SQLException e) {
            System.out.println("查找用户电影票失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return tickets;
    }

    // 检查座位是否已被购买
    public boolean isSeatBooked(String scheduleId, String seatNumber) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT COUNT(*) FROM tickets WHERE scheduleId = ? AND seatNumber = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, scheduleId);
            pstmt.setString(2, seatNumber);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            System.out.println("检查座位是否已购买失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    // 删除电影票
    public boolean deleteTicket(String id) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM tickets WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("删除电影票失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 获取所有电影票（用于管理员等角色查看）
    public List<Ticket> getAllTickets() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Ticket> tickets = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM tickets";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Ticket ticket = new Ticket();
                ticket.setId(rs.getString("id"));
                ticket.setScheduleId(rs.getString("scheduleId"));
                ticket.setMovieId(rs.getString("movieId"));
                ticket.setSeatNumber(rs.getString("seatNumber"));
                ticket.setPrice(rs.getDouble("price"));
                ticket.setUsername(rs.getString("username"));
                ticket.setPurchaseTime(rs.getString("purchaseTime"));
                tickets.add(ticket);
            }
        } catch (SQLException e) {
            System.out.println("获取所有电影票失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return tickets;
    }
}
